<?php

class AccountController extends Controller
{
	public $layout = 'default_login';
    
    public function init() {   
    }
    
    public function actionIndex()
    {    
        // Check if logged in already then redirect to homepage
        $security = new YWSAuthentication();
        if ($security->hasIdentity()) $this->redirect(array('Default/index'));
        
        $model = new PortalUsers(); 
        $this->performAjaxValidation($model);
        
        // POST processing
        if(isset($_POST['PortalUsers']))
        {
            $model->attributes=$_POST['PortalUsers'];
            // Validate User
            $username = $model->username;
            $password = md5($model->password);
            $security = new YWSAuthentication();
            // Authenticate
            $userinfo = $security->authenticate($username, $password);
            if ($userinfo != false) {                                
                $security->saveIdentity($userinfo);
                $this->redirect(array('Default/index'));                
            }
            else {
                // Open flash message                
                Yii::app()->user->setFlash('error',$security->auth_message);
                //$this->redirect(array('Account/index'));
            }
        }
        
        $this->render('index', array('model'=>$model));
    }
    
    protected function performAjaxValidation($model) 
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
    
    public function actionChangePassword() {
        $model = new ChangePasswordForm();
        
        if (isset($_POST['ChangePasswordForm'])) {
            $model->attributes=$_POST['ChangePasswordForm'];
            $model->validate();            
            $oldpass = md5(trim($model->oldpassword));
            $newpass = md5(trim($model->newpassword));
            $confirmpass = md5(trim($model->confirmpassword));
            $db = new PortalUsers();
            $criteria = new CDbCriteria();
            $criteria->condition = "username = '".YWSAuthentication::getIdentityName()."'";
            $userinfo = $db->find($criteria);
                        
            $flag = false;                             
            
            if (strcmp($userinfo->password, $oldpass) != 0) Yii::app()->user->setFlash('error','Mật khẩu cũ không chính xác');
            if (strcmp($newpass, $oldpass) == 0) Yii::app()->user->setFlash('error','Mật khẩu mới phải khác mật khẩu cũ');            
            if (strcmp($newpass, $confirmpass) != 0) Yii::app()->user->setFlash('error','Xác nhận mật khẩu không chính xác');
                        
            if (strcmp($newpass,$confirmpass) == 0 && strcmp($oldpass,$newpass) != 0) $flag = true;
            
            if ($flag == true) {
                echo "TRUE";
                // Change password                
                $userinfo->password = $newpass;
                $userinfo->update();
                $this->redirect(array('Default/index'));
            }
            else echo "FALSE";
        }
        
        $this->setPageTitle('Đổi mật khẩu cá nhân');
        $this->render('changepassword', array('model'=>$model));
    }
    
    public function actionLogout() {
        YWSAuthentication::logOut();
        $this->redirect(array('Default/index'));
    }
}